package com.xm.zhEnHelper.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.xm.zhEnHelper.dto.ColumDTO;
import com.xm.zhEnHelper.dto.FieldValue;
import com.xm.zhEnHelper.mapper.TableOperateMapper;
import com.xm.zhEnHelper.service.TableOperateService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
@RequiredArgsConstructor
public class TableOperateServiceImpl implements TableOperateService {

    private final TableOperateMapper tableOperateMapper;

    @Override
    public List<String> queryTableNames(String schemaName) {
        return tableOperateMapper.queryTableNames(schemaName);
    }

    @Override
    public List<ColumDTO> queryTableColumns(String schemaName,String tableName) {
        return tableOperateMapper.queryTableColumns(schemaName, tableName);
    }

    @Override
    public List<List<FieldValue>> queryTableData(String schemaName , String tableName, List<String> columnNames) {
        List<ColumDTO> columDTOS = this.queryTableColumns(schemaName, tableName);
        if (CollUtil.isEmpty(columDTOS)){
            log.warn("查询schema:{},table:{} 的字段信息为空，请检查表名或schema名称是否正确", schemaName,tableName);
            return new ArrayList<>();
        }
        List<Map<String, Object>> maps = tableOperateMapper.queryTableData(schemaName, tableName, columnNames);
        if (CollUtil.isEmpty(maps)){
            log.info("查询schema:{},table:{} 的数据为空", schemaName,tableName);
            return new ArrayList<>();
        }
        List<List<FieldValue>> result = new ArrayList<>(maps.size());
        for (Map<String, Object> map : maps) {
            if (CollUtil.isEmpty(map)){
                continue;
            }
            List<FieldValue> fieldValues = new ArrayList<>(map.size());
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                FieldValue fieldValue = new FieldValue();
                fieldValue.setFieldName(entry.getKey());
                fieldValue.setFieldValue(entry.getValue());
                for (ColumDTO columDTO : columDTOS) {
                    if (StrUtil.equals(columDTO.getFieldName(),fieldValue.getFieldName())){
                        fieldValue.setFieldType(columDTO.getFieldType());
                        fieldValue.setPrimaryKey(StrUtil.equals("PRI",columDTO.getFieldKey()));
                        break;
                    }
                }
                fieldValues.add(fieldValue);
            }
            result.add(fieldValues);
        }
        return result;
    }

    @Override
    public void updateTableData(String schemaName,String tableName, List<List<FieldValue>> dataList) {

        for (List<FieldValue> data : dataList) {
            FieldValue primaryKeyField = null;
            Map<String,Object> updateFields = new HashMap<>();
            for (FieldValue datum : data) {
                if (datum.isPrimaryKey()) {
                    primaryKeyField = datum; // 找到主键字段
                } else {
                    updateFields.put(datum.getFieldName(), datum.getFieldValue());
                }
            }
            if (CollUtil.isEmpty(updateFields)){
                log.warn("更新数据时，更新字段不能为空，跳过当前行");
                continue;
            }
            Assert.notNull(primaryKeyField, "更新数据时，主键字段不能为空");
            tableOperateMapper.updateTableData(schemaName,tableName, updateFields,primaryKeyField.getFieldName(),primaryKeyField.getFieldValue());
        }

    }
}
